﻿using Microsoft.AspNetCore.Mvc.Filters;
using System.Net;
using System.Net.Sockets;

namespace S.Services.SheepMale.Api
{
    /// <summary>
    /// 全局异常过滤器
    /// </summary>
    public class Exfiter : IExceptionFilter
    {
        private readonly ILogger<Exfiter> _logger;

        public Exfiter(ILogger<Exfiter> logger)
        {
            _logger = logger;
        }
        public void OnException(ExceptionContext context)
        {
            var hostName = Dns.GetHostName();
            var hostEntry = Dns.GetHostEntry(hostName);
            var ipaddress = hostEntry.AddressList.FirstOrDefault(ip=>ip.AddressFamily==AddressFamily.InterNetwork);

            _logger.LogError(ipaddress + context.Exception.StackTrace[..50] + "陈子平发生了异常");
            context.ExceptionHandled = true;
        }
    }

}
